﻿using System;
using System.Collections.Generic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Migration.Models;
using ControleRepresentante.Migration.Repositories;

namespace ControleRepresentante.Migration.Steps
{
    internal class FabricaMigrationStep : MigrationStepBase
    {
        public override void Migrate(Guid representanteId, MigrationStatus migrationStatus, ControleRepresentanteRepository controleRepresentanteRepository, ZTRepository ztRepository, Guid userId)
        {
            migrationStatus.CurrentStep = "Migrate Fabricas";

            int total = ztRepository.GetTotalFabricas();

            Guid observacaoAttributeId = CreateAttribute(representanteId, "Observação", AttributeType.Fabrica, controleRepresentanteRepository);
            Guid contatoAttributeId = CreateAttribute(representanteId, "Contato", AttributeType.Fabrica, controleRepresentanteRepository);

            IEnumerable<ZTFabrica> ztFabricas = ztRepository.GetFabricas();

            int index = 0;
            foreach (ZTFabrica ztFabrica in ztFabricas)
            {
                Guid fabricaId = GetFabricaId(ztFabrica.Id);
                controleRepresentanteRepository.CreateFabrica(representanteId, fabricaId, ztFabrica.RazaoSocial, ztFabrica.CNPJ, ztFabrica.InscricaoEstadual);
                controleRepresentanteRepository.CreateFabricaJolitex(fabricaId, ztFabrica.RazaoSocial.ToLower().Contains("jolitex"));
                controleRepresentanteRepository.CreateAttributeValue(fabricaId, observacaoAttributeId, ztFabrica.Observacao);
                controleRepresentanteRepository.CreateAttributeValue(fabricaId, contatoAttributeId, ztFabrica.Contato);
                controleRepresentanteRepository.CreateContato(fabricaId, ContatoType.TelefoneComercial, true, ztFabrica.Telefone);
                controleRepresentanteRepository.CreateContato(fabricaId, ContatoType.FaxComercial, true, ztFabrica.Fax);
                controleRepresentanteRepository.CreateContato(fabricaId, ContatoType.Email, true, ztFabrica.Email);
                controleRepresentanteRepository.CreateEndereco(fabricaId, EnderecoType.Comercial, true, ztFabrica.Endereco, ztFabrica.Bairro, ztFabrica.CEP, ztFabrica.Cidade, ztFabrica.Estado);
                migrationStatus.SetCurrentProgress(++index, total);
            }
        }
    }
}